package com.bdc.whatwhat.adapters;

import android.content.Context;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;

import com.bdc.whatwhat.R;
import com.bdc.whatwhat.modele.Notification;
import com.bdc.whatwhat.modele.WhatWhat;
import com.bdc.whatwhat.views.cell.DemandeAmiCell;
import com.bdc.whatwhat.views.cell.DemandeWhatWhatCell;
import com.bdc.whatwhat.views.cell.ReponseWhatWhatCell;
import com.bdc.whatwhat.views.cell.ViewCell;
import com.bdc.whatwhat.views.cell.WhatWhatCell;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * Created by gfimac on 21/07/2014.
 */
public class NotificationAdapter extends BaseAdapter {

    Context context;

    public interface NotificationAdapterDelegate{
        public void supprimer(int position);
    }

    List<Notification> notifications;
    Object[] params;

    public NotificationAdapter(Context context, List<Notification> notifications, Object...params) {
        this.context = context;
        this.notifications = notifications;
        this.params = params;
    }

    @Override
    public int getItemViewType(int position) {
        return notifications.get(position).getType();
    }

    @Override
    public int getViewTypeCount() {
        return Notification.NOMBRE_TYPES;
    }

    @Override
    public int getCount() {
        if (notifications == null)
            return 0;
        else
            return notifications.size();
    }

    @Override
    public Object getItem(int i) {
        if (notifications == null)
            return null;
        else {
            return notifications.get(i);
        }
    }

    @Override
    public long getItemId(int i) {
        return i;
    }

    @Override
    public View getView(int i, View reuseView, ViewGroup viewGroup) {
        View view = reuseView;
        Notification notification = (Notification) getItem(i);

        if (view == null) {
            switch (getItemViewType(i)) {
                case Notification.DEMANDE_AMI:
                    view = View.inflate(context, R.layout.cell_demande_ami, null);
                    break;
                case Notification.DEMANDE_WHATWHAT:
                    view = View.inflate(context, R.layout.cell_demande_whatwhat, null);
                    break;
                case Notification.REPONSE_WHATWHAT:
                    view = View.inflate(context, R.layout.cell_reponse_whatwhat, null);
                    break;
            }
        }

        ViewCell cell = (ViewCell)view.getTag();

        if (cell == null) {

            switch (getItemViewType(i)) {
                case Notification.DEMANDE_AMI:
                    cell = new DemandeAmiCell();
                    cell.setParams(params);
                    cell.construire(context, view, notification.getWhatter(), i);
                    break;
                case Notification.DEMANDE_WHATWHAT:
                    cell = new DemandeWhatWhatCell();
                    cell.setParams(params);
                    cell.construire(context, view, notification.getWhatter(), i);
                    break;
                case Notification.REPONSE_WHATWHAT:
                    cell = new ReponseWhatWhatCell();
                    cell.setParams(params);
                    cell.construire(context, view, notification.getWhatter(), i);
                    break;
            }

            view.setTag(cell);

        } else {
            switch (getItemViewType(i)) {
                case Notification.DEMANDE_AMI:
                    cell.construireObject(notification.getWhatter(), i);
                    break;
                case Notification.DEMANDE_WHATWHAT:
                    cell.construireObject(notification.getWhatter(), i);
                    break;
                case Notification.REPONSE_WHATWHAT:
                    cell.construireObject(notification.getWhatter(), i);
                    break;
            }
        }

        cell.apparaitre();

        return view;
    }

}
